package com.huike.pmps.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huike.pmps.model.dto.member.MemberRoleDTO;
import com.huike.pmps.model.entity.Member;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;


public interface MemberDao extends BaseMapper<Member> {

    /**
     * 根据账户名 查询用户详细信息
     *
     * @param memberName 账户名
     * @return 会员
     */
    @Select(" select * from es_member where member_name = #{memberName}")
    @Results(id = "memberMap", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "memberName", column = "member_name"),
            @Result(property = "password", column = "password"),
            @Result(property = "name", column = "name"),
            @Result(property = "mobile", column = "mobile"),
            @Result(property = "email", column = "email"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "regionId", column = "region_id"),
            @Result(property = "regionName", column = "region_name"),
            @Result(property = "provinceId", column = "province_id"),
            @Result(property = "provinceName", column = "province_name"),
            @Result(property = "state", column = "state"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "roles", column = "id", many = @Many(select = "com.huike.pmps.dao.RoleDao.findByMemberId", fetchType = FetchType.EAGER))
    })
    Member findByMemberName(String memberName);


    /**
     * 根据ID名用户详情
     *
     * @param memberId 用户名
     * @return
     */
    @Select(" select * from es_member where id = #{memberId}")
    @ResultMap("memberMap")
    Member findById(int memberId);


    @Select({
            "<script>",
            "select * from (select * from es_member  <where> <if test='regionId!=null and regionId!=0'> region_id=#{regionId}</if></where>) m",
            "inner join es_member_role mr on m.id=mr.member_id",
            "inner join es_role r on mr.role_id=r.id and r.id=4",
            "</script>"
    })
    @Results(id = "memberMap2",value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "memberName", column = "member_name"),
            @Result(property = "password", column = "password"),
            @Result(property = "name", column = "name"),
            @Result(property = "mobile", column = "mobile"),
            @Result(property = "email", column = "email"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "regionId", column = "region_id"),
            @Result(property = "regionName", column = "region_name"),
            @Result(property = "provinceId", column = "province_id"),
            @Result(property = "provinceName", column = "province_name"),
            @Result(property = "state", column = "state"),
            @Result(property = "createTime", column = "create_time"),
    })
    List<Member> finByRegionId(Integer regionId);



    @Select({
            "<script>",
            "select * from es_member  m",
            "inner join es_member_role mr on m.id=mr.member_id",
            "inner join es_role r on mr.role_id=r.id and r.id=4",
            "</script>"
    })
    @ResultMap("memberMap2")
    List<Member> findAllCourseCharge();


    /**
     * 根据用户memberId，删除(用户角色关系表)中的数据
     *
     * @param memberId
     * @return
     */
    @Delete(" delete from es_member_role where member_id= #{memberId} ")
    int deleteMemberAndRoleRelationByMemberId(int memberId);


    @Insert({
            "<script>",
            "insert into es_member_role(member_id, role_id) values ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.memberId}, #{item.roleId})",
            "</foreach>",
            "</script>"
    })
    int insertMemberAndRoleRelation(List<MemberRoleDTO> list);
}
